﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TouristAgencyDataLayer
{
    public class Destinations
    {
        public static void CreateDestination(string dest)
        {
            if (!string.IsNullOrEmpty(dest))
            {
                TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

                Destination d = new Destination()
                {
                    Destination1 = dest
                };

                context.Destinations.InsertOnSubmit(d);
                context.SubmitChanges();
            }
        }

        public static void UpdateDestination(int destId, string dest)
        {
            if (!string.IsNullOrEmpty(dest))
            {
                TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

                var res =
                    (from d in context.Destinations
                    where d.DestinationId == destId
                    select d).FirstOrDefault();

                if (res != null)
                {
                    res.Destination1 = dest;
                    context.SubmitChanges();
                }
            }
        }

        public static void DeleteDestination(int destId)
        {
           
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            Destination des =
                (from d in context.Destinations
                 where d.DestinationId == destId
                 select d).FirstOrDefault();

            if (des != null)
            {
                context.Destinations.DeleteOnSubmit(des);
                context.SubmitChanges();
            }
        }

        public static IQueryable<Destination> GetDestinations()
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            return context.Destinations;
        }

        public static Destination GetDestinationById(int id)
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            Destination des =
                (from d in context.Destinations
                 where d.DestinationId == id
                 select d).FirstOrDefault();

            if (des != null)
            {
                return des;
            }
            else
            {
                throw new ArgumentException("Invalid destination id");
            }
        }

        public static bool TourWithThatDestination(int destId)
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            var tour =
                (from t in context.Tours
                 where t.DestinationId == destId
                 select t).FirstOrDefault();

            if (tour != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
